<template>
  <el-dialog :visible.sync="dialog" :close-on-click-modal="false" :before-close="cancel" :title="isAdd ? '新增部门' : '编辑部门'" width="450px">
    <el-form ref="form" :inline="true" :model="form" size="small" :show-message="false" label-width="80px">
      <el-form-item label="名称" prop="name" required>
        <el-input v-model="form.name" style="width: 300px;" />
      </el-form-item>
      <el-form-item label="状态" prop="enabled">
        <el-radio-group v-model="form.enabled">
          <el-radio :label="true">启用</el-radio>
          <el-radio :label="false">停用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="上级部门" prop="parentId">
        <treeselect
          v-model="form.parentId"
          :normalizer="normalizer"
          :options="departments"
          style="width: 300px;"
          placeholder="选择上级类目"
          no-options-text="无数据"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDepartmentTree, addDepartment, editDepartment } from '@/api/system/department'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  components: { Treeselect },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      loading: false,
      dialog: false,
      departments: null,
      isTop: true,
      form: { name: '', enabled: true, parentId: 0 },
      normalizer(node) {
        // 去掉children为空的children属性
        if (!node.children || (node.children && !node.children.length)) {
          delete node.children
        }
        return {
          id: node.id,
          label: node.name,
          children: node.children
        }
      }
    }
  },
  mounted() {
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else {
            this.doEdit()
          }
        } else {
          return false
        }
      })
    },
    doAdd() {
      addDepartment(this.form).then(res => {
        this.resetForm()
        this.$notify({ title: '添加成功', type: 'success', duration: 2500 })
        this.$parent.loadData()
      }).finally(() => {
        this.loading = false
      })
    },
    doEdit() {
      editDepartment(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.$parent.loadData()
      }).finally(() => {
        this.loading = false
      })
    },
    resetForm() {
      this.dialog = false
      this.$nextTick(() => {
        this.$refs['form'].resetFields()
      })
    },
    async getDepartments() {
      await getDepartmentTree().then(res => {
        this.departments = []
        const department = { id: 0, name: '顶级部门', children: [] }
        department.children = res.data
        this.departments.push(department)
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
